import math
for _ in range(int(input())):
n,k=map(int,input().split())
p=list(map(int,input().split()))
c=0
for i in range(n):
if (p[i]-i-1)%k!=0:
c+=1
c=math.ceil(c/2)
if c==0:
print(0)
elif c==1:
print(1)
else:
print(-1)
#include <bits/stdc++.h>
using namespace std;
int main(){
int t;
cin>>t;
while(t--){
int n,k,c=0,x;
cin>>n>>k;
for(int i=1;i<n+1;i++){
cin>>x;
c+=i%k!=x%k;
}
cout<<((c>2)?-1:c/2)<<endl;
}
}
1462A - Favorite Sequence | 1445B - Elimination |
1656C - Make Equal With Mod | 567A - Lineland Mail |
1553A - Digits Sum | 1359B - New Theatre Square |
766A - Mahmoud and Longest Uncommon Subsequence | 701B - Cells Not Under Attack |
702A - Maximum Increase | 1656D - K-good |
1426A - Floor Number | 876A - Trip For Meal |
1326B - Maximums | 1635C - Differential Sorting |
961A - Tetris | 1635B - Avoid Local Maximums |
20A - BerOS file system | 1637A - Sorting Parts |
509A - Maximum in Table | 1647C - Madoka and Childish Pranks |
689B - Mike and Shortcuts | 379B - New Year Present |
1498A - GCD Sum | 1277C - As Simple as One and Two |
1301A - Three Strings | 460A - Vasya and Socks |
1624C - Division by Two and Permutation | 1288A - Deadline |
1617A - Forbidden Subsequence | 914A - Perfect Squares |